package org.loofer.ext.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    public static final String DEFAULT_PATTERN = "yyyy-MM-dd_HH-mm-ss.SSS";
    private static final ThreadLocal<SimpleDateFormat> sDefaultDateFormat = new ThreadLocal<SimpleDateFormat>() {
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat(DateUtils.DEFAULT_PATTERN);
        }
    };

    private DateUtils() {
    }

    public static String getDate() {
        return getDate(System.currentTimeMillis());
    }

    public static String getDate(long timeInMillis) {
        return ((SimpleDateFormat) sDefaultDateFormat.get()).format(new Date(timeInMillis));
    }

    public static String getDate(String pattern) {
        return getDate(pattern, System.currentTimeMillis());
    }

    public static String getDate(String pattern, long timeInMillis) {
        return new SimpleDateFormat(pattern).format(new Date(timeInMillis));
    }

    public static long getStartOfDay(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return calendar.getTimeInMillis();
    }

    public static String getDateDesc(Date date) {
        long time = new Date().getTime() - date.getTime();
        String str = "很久了";
        String mmDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
        if ((time / 1000) / 60 <= 3) {
            return "刚刚";
        }
        if ((time / 1000) / 60 > 3 && (time / 1000) / 60 < 60) {
            return ((time / 1000) / 60) + "分钟前";
        }
        if (((time / 1000) / 60) / 60 >= 1 && ((time / 1000) / 60) / 60 < 24) {
            return (((time / 1000) / 60) / 60) + "小时前";
        }
        if ((((time / 1000) / 60) / 60) / 24 >= 1 && (((time / 1000) / 60) / 60) / 24 < 2) {
            return "1天前";
        }
        if ((((time / 1000) / 60) / 60) / 24 >= 2 && (((time / 1000) / 60) / 60) / 24 < 3) {
            return "2天前";
        }
        if ((((time / 1000) / 60) / 60) / 24 >= 3) {
            return mmDate;
        }
        return str;
    }
}
